<template>
  <div class="video-list">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>已生成的视频</span>
        </div>
      </template>
      <el-table :data="videos" style="width: 100%">
        <el-table-column prop="id" label="ID" width="180" />
        <el-table-column prop="status" label="状态" width="120">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="创建时间" width="180">
          <template #default="scope">
            {{ formatDate(scope.row.createdAt) }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              v-if="scope.row.status === 'completed'"
              type="primary"
              size="small"
              @click="playVideo(scope.row)"
            >
              播放
            </el-button>
            <el-button
              v-if="scope.row.status === 'completed'"
              type="success"
              size="small"
              @click="downloadVideo(scope.row)"
            >
              下载
            </el-button>
            <el-button
              v-if="scope.row.status === 'processing'"
              type="info"
              size="small"
              @click="checkProgress(scope.row)"
            >
              查看进度
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 视频播放对话框 -->
    <el-dialog
      v-model="dialogVisible"
      title="视频播放"
      width="80%"
      :before-close="handleClose"
    >
      <video
        v-if="currentVideo"
        :src="currentVideo.url"
        controls
        style="width: 100%"
      ></video>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

interface Video {
  id: string
  status: string
  createdAt: string
  updatedAt: string
  url?: string
}

const videos = ref<Video[]>([])
const dialogVisible = ref(false)
const currentVideo = ref<Video | null>(null)

// 获取视频列表
const fetchVideos = async () => {
  try {
    const response = await axios.get('/api/videos')
    if (response.data.success) {
      videos.value = response.data.data
    }
  } catch (error) {
    ElMessage.error('获取视频列表失败')
    console.error('获取视频列表失败:', error)
  }
}

// 格式化日期
const formatDate = (date: string) => {
  return new Date(date).toLocaleString()
}

// 获取状态类型
const getStatusType = (status: string) => {
  switch (status) {
    case 'completed':
      return 'success'
    case 'processing':
      return 'warning'
    case 'failed':
      return 'danger'
    default:
      return 'info'
  }
}

// 获取状态文本
const getStatusText = (status: string) => {
  switch (status) {
    case 'completed':
      return '已完成'
    case 'processing':
      return '处理中'
    case 'failed':
      return '失败'
    default:
      return '未知'
  }
}

// 播放视频
const playVideo = (video: Video) => {
  currentVideo.value = video
  dialogVisible.value = true
}

// 下载视频
const downloadVideo = (video: Video) => {
  if (video.url) {
    window.open(video.url, '_blank')
  }
}

// 检查进度
const checkProgress = async (video: Video) => {
  try {
    const response = await axios.get(`/api/videos/${video.id}/progress`)
    if (response.data.success) {
      ElMessage.info(`当前进度: ${response.data.data.progress}%`)
    }
  } catch (error) {
    ElMessage.error('获取进度失败')
    console.error('获取进度失败:', error)
  }
}

// 关闭对话框
const handleClose = () => {
  dialogVisible.value = false
  currentVideo.value = null
}

// 组件挂载时获取视频列表
onMounted(() => {
  fetchVideos()
})
</script>

<style scoped>
.video-list {
  margin: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 